
% fits an edge using a scaled sigmoidal function
function r = refineEdgePosition_v1_2(y)

doplot = false;

x = (0:(length(y)-1))';
y = y - min(y);
%normalize y:
y = y/max(y);
po(1) = max(y);
po(2) = find(y>.5,1);
po(3) = 1;

lb(1) = .5*po(1);
lb(2) = 1;
lb(3) = .1;
ub(1) = 2*po(1);
ub(2) = max(x);
ub(3) = 12;
opts = optimset('display','off');

F = @(p) p(1).*1./(1+exp(-(x-p(2))/p(3)))-y;
r = lsqnonlin(F,po,lb,ub,opts);
if doplot
    figure
    plot(x,y,'.')
    hold on
    plot(x,F(r)+y)
end

